package com.teatime.client.user;

import java.io.UnsupportedEncodingException;

import android.content.Intent;
import android.view.View;

import com.teatime.client.Constants;
import com.teatime.client.R;
import com.teatime.client.TLog;
import com.teatime.client.activity.TActivity;
import com.teatime.client.net.http.HttpRequestParams;
import com.teatime.client.net.http.THttpClient;
import com.teatime.client.net.http.THttpClientListener;

public class CreatePasswordActivity extends TActivity implements THttpClientListener {

	final private String Action = "/videos/reg3.php";
	
	@Override
	public int getContentViewId() {
		// TODO Auto-generated method stub
		return R.layout.activity_create_password;
	}
	
	public void submitPassword(View v) {
		THttpClient client = new THttpClient(Constants.ServerHost + Action, this, this);
		HttpRequestParams params = new HttpRequestParams();
		String password = getTextFromEdit(R.id.txt_password);
		if (password == null || password == "") {
			showErrMsg(R.string.err_invalid_password);
			return;
		}

		String password2 = getTextFromEdit(R.id.txt_password2);
		if (!password2.equals(password)) {
			showErrMsg(R.string.err_password_inconsistent);
			return;
		}

		params.add("password", password);
		try {
			client.post(params);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			TLog.e("failed in httpclient post, " + e.toString());
			showErrMsg(R.string.err_internet);
		}
		//startActivity("user.Register2Activity");
	}

	@Override
	public void onGetResponse(int statusCode, String response) {
		// TODO Auto-generated method stub
		TLog.v("response status code: " + statusCode);
		if (statusCode == 200) {
			if (response.startsWith("ok")) {
				Intent currIntent = getIntent();
				String mobile = currIntent.getStringExtra("regMobile");
				getApp().setUserName(mobile);
				getApp().setIsBusLogin(true);
				TLog.v("register finish, mobile: " + mobile);
				startActivity(".user.RegisterFinActivity");
				finish();
			}
			else {
				showErrMsg(response.trim());
			}
		}
		else {
			showErrMsg(R.string.err_server_exception);
		}
	}

	@Override
	public void onHttpError(String errmsg) {
		// TODO Auto-generated method stub
		showErrMsg(R.string.err_internet);
	}

	
}
